package com.hey.amqpdemo.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException;
import org.springframework.amqp.rabbit.support.Delivery;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @ClassName PayConsumer
 * @Author hey
 * @Date 2019/5/9 11:13
 **/
@Component
public class PayConsumer {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = "pay1Queue")
    public void consumer(Message message, Channel channel) throws IOException {
        String aa = new String(message.getBody(),"utf-8");
        System.out.println("收到消息："+aa);
        long tag = message.getMessageProperties().getDeliveryTag();
        System.out.println("消息ID："+tag);
        System.out.println("消费完成，开始确认");
//        throw new NullPointerException("xxxx");
//        channel.basicNack(tag,false,true); //后面参数为是否批量确认，为否
    }

    @RabbitListener(queues = "deadQueue")
    public void deadConsumer(Message message, Channel channel) throws IOException {
        System.out.println("死信队列收到消息："+new String(message.getBody(),"utf-8"));
        long tag = message.getMessageProperties().getDeliveryTag();
        System.out.println("消息ID："+tag);
        System.out.println("消费完成，开始确认");

//        throw new NullPointerException("xxxx");
//        channel.basicAck(tag,false); //后面参数为是否批量确认，为否
    }
}
